package prj.carmap.mobile.activity;

import java.util.Vector;

import prj.carmap.mobile.CarmapApplication;
import prj.carmap.mobile.R;
import prj.carmap.mobile.process.LoginProcess;
import prj.carmap.mobile.process.SellerTypeProcess;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 登录界面
 * 
 */

public class LoginActivity extends Activity {
	static final String TAG = "LoginActivity";
	private SharedPreferences mPreferences;

	/**
	 * 切换到登录界面
	 * 
	 * @param mContext
	 *            当前容器
	 */
	public static void RedirectToLoginActivity(Context mContext) {
		Intent intent = new Intent(mContext, LoginActivity.class);
		mContext.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		CarmapApplication.currentPage = "mine";
		CarmapApplication.addActivity(this);
		setContentView(R.layout.activity_login);

		// new UpdateVersionTask(this).checkVersion();

		init();
		// initMenuItem();
		// initActionBar();
	}

	private void init() {
		mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		// 登录界面和处理器绑定
		Button btnLogin = (Button) findViewById(R.id.btnLogin);
		LoginProcess loginP = new LoginProcess();
		loginP.setBtnLogin(btnLogin);
		loginP.setContext(this);
		loginP.setTxtCardNo((EditText) findViewById(R.id.txtCardNo));
		loginP.setTxtPassword((EditText) findViewById(R.id.txtPassword));
		loginP.setTxtPhone((EditText) findViewById(R.id.txtPhone));
		loginP.setTxtModels((EditText) findViewById(R.id.txtModels));
		loginP.setTxtLicense((EditText) findViewById(R.id.txtLicense));

		loginP.getTxtCardNo().setText(mPreferences.getString("cardNo", ""));
		loginP.getTxtPassword().setText(mPreferences.getString("password", ""));
		loginP.getTxtPhone().setText(mPreferences.getString("phoneNum", ""));
		loginP.getTxtModels().setText(mPreferences.getString("txtModels", ""));
		loginP.getTxtLicense()
				.setText(mPreferences.getString("txtLicense", ""));

		btnLogin.setOnClickListener(loginP);
	}

	@Override
	protected void onResume() {
		super.onResume();

		CarmapApplication.currentPage = "mine";
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Vector<Activity> ac = CarmapApplication.activities;
			for (Activity a : ac) {
				a.finish();
			}
			android.os.Process.killProcess(android.os.Process.myPid());
			return true;
		}
		return true;
	}
}
